package com.qf.mrmd.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qf.mrmd.dao.ShopDao;
import com.qf.mrmd.dao.SubjectDao;
import com.qf.mrmd.entity.Order;
import com.qf.mrmd.dao.OrderDao;
import com.qf.mrmd.entity.Shop;
import com.qf.mrmd.entity.Subject;
import com.qf.mrmd.result.ResponseCode;
import com.qf.mrmd.result.ResponseData;
import com.qf.mrmd.service.OrderService;
import com.qf.mrmd.util.Sms;
import org.apache.http.protocol.ResponseDate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Order)表服务实现类
 *
 * @author makejava
 * @since 2021-07-14 14:41:30
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao orderDao;

    @Resource
    private ShopDao shopDao;

    @Resource
    private SubjectDao subjectDao;

    /**
     * 通过ID查询单条数据
     *
     * @param oId 主键
     * @return 实例对象
     */
    @Override
    public Order queryById(Integer oId) {
        return this.orderDao.queryById(oId);
    }


    @Override
    public ResponseData queryAllByLimit(Integer offset, Integer limit) {

        if (offset == null || limit == null){
            offset = 0;
            limit = 10;
        }else {
            offset = (offset-1)*limit;
        }
        List<Order> orders = orderDao.queryAllByLimit(offset, limit);
        Integer count = orderDao.countOrder();
        return new ResponseData("0","success",orders);
    }

    /**
     * 新增数据
     *
     * @param order 实例对象
     * @return 实例对象
     */
    @Transactional
    @Override
    public ResponseData insert(Order order) {
        //添加事务，发送阿里云短信
        try {
            String code = Sms.SmsCode(order.getTel());
            if (code ==null){
                return new ResponseData(ResponseCode.FAILED);
            }
            if (order.getsName()!=null && order.getSuName()!=null){
                Shop shop = shopDao.queryByIdOrName(order.getsName());
                Subject subject = subjectDao.queryByIdOrName(order.getSuName());
                order.setsName(shop.getName());
                order.setSuName(subject.getName());
            }
            order.setSubtime(new Date());
            order.setStatus(0);
            order.setCode(code);
            int i = this.orderDao.insert(order);
            if (i>0){
                return new ResponseData(ResponseCode.SUCCESS);
            }
            return new ResponseData(ResponseCode.FAILED);
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseData(ResponseCode.FAILED);
        }


    }

    /**
     * 修改数据
     *
     * @param order 实例对象
     * @return 实例对象
     */
    @Transactional
    @Override
    public ResponseData update(Order order) {
        if (order.getsName()!=null && order.getSuName()!=null){
            Shop shop = shopDao.queryByIdOrName(order.getsName());
            Subject subject = subjectDao.queryByIdOrName(order.getSuName());
            order.setsName(shop.getName());
            order.setSuName(subject.getName());
        }

        int i = this.orderDao.update(order);
        if (i>0){
            return new ResponseData(ResponseCode.SUCCESS);
        }else{
            return new ResponseData(ResponseCode.FAILED);
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param oId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer oId) {
        return this.orderDao.deleteById(oId) > 0;
    }

    @Override
    public ResponseData countOrder() {
        Integer i = orderDao.countOrder();
        return new ResponseData(ResponseCode.SUCCESS,i);
    }

    @Transactional
    @Override
    public ResponseData deleteBatchById(String ids) {
        JSONObject jsonObject = JSONObject.parseObject(ids);
        JSONArray ids1 = jsonObject.getJSONArray("ids");
        Integer i = orderDao.deleteBatchById(ids1);
        if (i>0){
            return new ResponseData(ResponseCode.SUCCESS);
        }else {
            return new ResponseData("401","操作失败，请刷新后再试");
        }
    }

    @Override
    public ResponseData queryByLike(Order order, Integer page, Integer limit) {
        if (page == null || limit == null){
            page = 0;
            limit = 10;
        }else {
            page = (page-1)*limit;
        }
        List<Order> orderList = orderDao.queryByLike(order,page,limit);
        Integer i = orderDao.queryByLikeCount(order);
        return new ResponseData("0","success",orderList,i);
    }
}
